﻿namespace instamuz.Domain.Entities
{
    using System.Collections.Generic;
    using IndyCode.Infrastructure.Domain;
    using JetBrains.Annotations;

    public class Account : IEntity
    {
        [NotNull] 
        private readonly ICollection<Post> _posts = new List<Post>();

        [NotNull]
        private readonly ICollection<Account> _following = new List<Account>();

        [NotNull]
        private readonly ICollection<Account> _followers = new List<Account>();

        public virtual int Id { get; set; }
        
        [UsedImplicitly]
        protected Account()
        {
        }

        public Account(string email, string login)
        {
            Email = email;
            Login = login;
        }

        [NotNull]
        public virtual string Login { get; set; }

        [NotNull]
        public virtual Password Password { get; protected set; }

        [NotNull]
        public virtual string Email { get; protected set; }

        [NotNull]
        public virtual IEnumerable<Post> Posts
        {
            get { return _posts; }
        }

        [NotNull]
        public virtual IEnumerable<Account> Following
        {
            get { return _following; }
        }

        [NotNull]
        public virtual IEnumerable<Account> Followers
        {
            get { return _followers; }
        }

        public virtual void SetPassword(string password)
        {
            Password = new Password(password);
        }

        public virtual void SetEmail(string email)
        {
            Email = email;
        }

        public virtual void Post(Post post)
        {
            post.SetAccount(this);
            _posts.Add(post);
        }

        public virtual void Follow(Account account)
        {
            if (account == this)
                return;

            if (_following.Contains(account))
                return;

            _following.Add(account);
            account.AddFollower(this);
        }

        protected virtual void AddFollower(Account account)
        {
            if (_followers.Contains(account))
                return;

            _followers.Add(account);
        }
    }
}